package com.ca.ui.core.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.ca.ui.core.R;
import com.ca.ui.core.bean.MainHeader;
import com.ca.ui.core.bean.PaperInfo;
import com.ca.ui.core.linstener.BannerCardOnPageChangeListener;
import com.ca.ui.core.util.ActivityStarter;
import com.ca.ui.core.view.card.PaperCard;
import com.ca.ui.core.view.header.MainHeaderView;

import java.util.ArrayList;
import java.util.List;

public class HasHeaderPaperAdapter extends RecyclerView.Adapter<HasHeaderPaperAdapter.PaperHolder> {

    private Activity activity;
    private MainHeader mainHeader;
    private List<PaperInfo> list;

    public int mainHeaderHeight;

    public HasHeaderPaperAdapter(MainHeader mainHeader, List<PaperInfo> list) {
        this.mainHeader = mainHeader == null ? new MainHeader() : mainHeader;
        this.list = list == null ? new ArrayList<>() : list;
        this.list.add(0, null);
    }

    @NonNull
    @Override
    public PaperHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.paper_item_layout, parent, false);
        return new PaperHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onBindViewHolder(@NonNull PaperHolder holder, int position) {

        if (position == 0) {

            holder.mainHeaderView.setVisibility(View.VISIBLE);
            holder.paperCard.setVisibility(View.GONE);

            holder.mainHeaderView.bannerCard.setImageUrlArray(mainHeader.getBannerUrlArray());
            holder.mainHeaderView.clubSquareListView.setList(mainHeader.getMiniClubInfoList());

            holder.mainHeaderView.bannerCard.addOnPageChangeListener(new BannerCardOnPageChangeListener(
                    activity,
                    holder.mainHeaderView.bannerCard.imageView,
                    mainHeader.getBannerUrlArray()));

            holder.mainHeaderView.post(() -> {
                mainHeaderHeight = holder.mainHeaderView.getMeasuredHeight();
            });
        }
        else {

            holder.mainHeaderView.setVisibility(View.GONE);
            holder.paperCard.setVisibility(View.VISIBLE);

            PaperInfo paperInfo = list.get(position);
            holder.paperCard.setPaper(paperInfo);
            if (activity != null) {
                holder.paperCard.imageView.setOnClickListener(v -> {
                    ActivityStarter.startImageActivity(
                            activity,
                            paperInfo.getPaperImageUrlArray(),
                            0);
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setList(List<PaperInfo> list) {

        if (list == null) {
            list = new ArrayList<>();
        }

        if (!this.list.equals(list)) {
            this.list = list;
            this.list.add(0, null);
            notifyDataSetChanged();
        }
    }

    /**
     * 向尾部追加新的数据
     * @param list 传入的列表为新的数据列表
     */
    public void appendList(List<PaperInfo> list) {

        if (list == null) {
            return;
        }

        int startPosition = getItemCount();

        //  添加数据
        this.list.addAll(list);

        //  如果没有新的数据添加，弹出提示
        if (startPosition == this.list.size()) {
            Toast.makeText(activity, "暂时没有更多数据了", Toast.LENGTH_SHORT).show();
        }

        notifyItemRangeInserted(startPosition, this.list.size() - startPosition);
    }

    /**
     * 向头部追加新的数据
     * @param list 传入的列表为新的数据列表
     */
    public void appendListToTop(List<PaperInfo> list) {

        if (list == null) {
            return;
        }

        //  添加数据
        for (PaperInfo info : list) {
            this.list.add(1, info);
        }

        //  如果没有新的数据添加，弹出提示
        if (list.size() == 0) {
            Toast.makeText(activity, "暂时没有更多数据了", Toast.LENGTH_SHORT).show();
        }

        notifyItemRangeInserted(1, list.size());
    }

    public MainHeader getMainHeader() {
        return mainHeader;
    }

    public void setMainHeader(MainHeader mainHeader) {
        this.mainHeader = mainHeader == null ? new MainHeader() : mainHeader;
        notifyItemChanged(0);
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public static class PaperHolder extends RecyclerView.ViewHolder {

        public MainHeaderView mainHeaderView;
        public PaperCard paperCard;

        public PaperHolder(@NonNull View itemView) {
            super(itemView);
            mainHeaderView = itemView.findViewById(R.id.mainHeader);
            paperCard = itemView.findViewById(R.id.card_paper);
        }
    }
}
